var createItemJs = {
  addAttribute : function(attrName, attrValue){
    var attributeChilds = document.getElementById('attributeChilds');
    var item = document.createElement('treeitem');
    var row = document.createElement('treerow');
    var c1 = document.createElement('treecell');
    var c2 = document.createElement('treecell');
    c1.setAttribute('label', attrName);
    c2.setAttribute('label', attrValue);
    row.appendChild(c1);
    row.appendChild(c2);
    item.appendChild(row);
    attributeChilds.appendChild(item);
  },

  removeSelectedAttribute : function(){
    var attributeTree = document.getElementById('attributeTree');
    var attributeChilds = document.getElementById('attributeChilds');
    //alert(attributeTree.currentIndex);
    var childNodes = attributeChilds.childNodes;
    attributeChilds.removeChild(childNodes[attributeTree.currentIndex]);
  },

  inputAttribute : function(){
  	var retVals = { attrName : null, attrValue : null};
		openDialog("attributeDialog.xul", "dlg-attribute", "modal,close=no", retVals);
		if(retVals.attrName != null){
			this.addAttribute(retVals.attrName,retVals.attrValue);
		}
  },

  init : function(){
	  var recentUri = getRecentUri();
	  var URIs = parseUri(recentUri);
	  document.getElementById('txtCorpus').value = URIs.directory;
	  document.getElementById('txtName').value = URIs.file;
  },

  doCancel : function(){
  	return true;
  },

  doOK : function(){
  	var stringBundle = document.getElementById("sbLasuli");
  	var currentSetting = getCurrentSetting();
  	var recentUri = getRecentUri();
    debug("[createItem.js::doOK]] currentSetting:" + currentSetting.toJSONString(),4);

  	var wdCorpus = document.getElementById('txtCorpus').value;
  	var wdSource = document.getElementById('txtName').value;

  	if('undefined' == typeof wdSource || wdSource == ""){
    	alert(stringBundle.getString("createItem.error.sourcenameisnull"));
    	return false;
  	}
		if(wdCorpus.startsWith("/")) wdCorpus = wdCorpus.substr(1);
  	if(wdCorpus.endsWith("/")) wdCorpus = wdCorpus.substring(0,wdCorpus.length-1);

  	var dav = new WebDav(wdCorpus, wdSource, currentSetting.webdav);
		debug("[createItem.js:doOK] wdCorpus:" + wdCorpus + ", wdSource:" + wdSource, 3);
		var davUrl = currentSetting.webdav.url + wdCorpus + "/" + wdSource;
		debug("[createItem.js:doOK] davUrl:" + davUrl, 3);
		//var docHtml = getRecentContent();
		//var docHtml = getContents(recentUri);

		//debug("[createItem.js::httpGet] docHtml:" + docHtml, 4);
	  /*var req = new customRequest('GET', recentUri, "");
	  var http = new HTTPCore();
	  var res = http.doOperation(req);
	  debug("[createItem.js::httpGet] recentUri:" + recentUri, 4);
	  debug("[createItem.js::httpGet] Response:" + http.responseXML, 4);
	  if(!res) return false;
  	var docHtml = http.response;
    */
    if(recentUri == null || recentUri == "")
    {
    	alert(stringBundle.getString("createItem.error.documentisnull"));
    	return false;
    }
    var tmpFile = saveToLocal(recentUri);
    //tmpFile = "c:\\download.jpg";
    debug("[createItem.js::httpGet] Response:" + tmpFile, 4);
    dav.mkdir(davUrl);
    var res = putToRemote(tmpFile, davUrl);
    if(!res){
    	alert(stringBundle.getString("createItem.error.webdaverror") + "\nUrl:" + davUrl);
    	return false;
    }

    if(wdSource.indexOf(".") > 0) wdSource = wdSource.substring(0,wdSource.indexOf("."));
    var entityUrl = currentSetting.hypertopic.url + "entity/" + wdCorpus + "/" + wdSource;
    var oEntity = new Entity();
    oEntity.uri = entityUrl;
    debug("[createItem.js:doOK] entityUrl: " + entityUrl, 3);
    oEntity.addResource("source",davUrl);
    oEntity.addResource("original",recentUri);
    var attributeChilds = document.getElementById('attributeChilds');
  	var rows = attributeChilds.childNodes;
  	for(var i=0; i < rows.length; i++){
  		var cells = rows[i].firstChild.childNodes;
  		oEntity.addAttribute(cells[0].getAttribute('label'),cells[1].getAttribute('label'));
  	}
  	debug("[createItem.js:doOK] entity xml: " + oEntity.toXml(), 4);
    var res = oEntity.httpPut();
    if(res == false){
    	alert(stringBundle.getString("createItem.error.hterror"));
	  	return false;
		}else
			alert(stringBundle.getString("createItem.ok"));
  }
}